package logic

import (
	"MyBlogs/dao/mysql"
	"MyBlogs/models"
	"MyBlogs/pkg/session"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

// 寻找所有 用户信息
func FindAllUser(context *gin.Context) {
	user := mysql.FindAllUser()
	context.JSON(200, gin.H{
		"user": user,
	})
}

func StateUser(context *gin.Context) {
	state, _ := context.GetQuery("state")
	idStr, _ := context.GetQuery("user_id")
	id, _ := strconv.ParseInt(idStr, 10, 64)
	mysql.ModStateUser(id, state)
}

// 判断用户此时 是否登录
func IsLogin(context *gin.Context) {
	var id int64

	val := session.GetSession(context)
	if n, ok := val["id"]; ok {
		id = n.(int64)
	}

	if id < 1 {
		context.JSON(200, gin.H{
			"data": "登录",
		})
		return
	} else {
		context.JSON(200, gin.H{
			"data": "退出登录",
		})
		return
	}

}

// 用户 退出登录
func LogOut(context *gin.Context) {
	session.FlushSession(context)
}

// 用户 提交意见
func AdviceAdd(context *gin.Context) {
	var info *models.Feedback
	if err := context.ShouldBindJSON(&info); err != nil {
		fmt.Println("用户意见数据绑定错误！", err)
		return
	}
	info.CreateTime = time.Now().Format("2006-01-02 15:04:05")
	info.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
	mysql.AdviceAdd(info)
	context.JSON(200, nil)

}
